//
//  BIZLivePlayBackCell.m
//  biz
//
//  Created by zhangfuwei on 2017/3/23.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZLivePlayBackCell.h"

static NSString * kVideoPauseImg = @"movie_sitcomList_pause";
static NSString * kVideoPlayImg = @"movie_sitcomList_play";

static CGFloat kEdgeMargin = 10;
static CGFloat kMargin = 5;
static CGFloat kChapterLabW = 20;
static CGFloat kDurationLabelH = 20;
static CGFloat kDurationLabelW = 50;
//static CGFloat kChapterLabelW = 60;

@implementation BIZLivePlayBackCell {
    UIView  *_topVerticalLine;
    UIView  *_middleCircle;
    UIView  *_bottomVerticalLine;
    UILabel *_dateLabel;
    UILabel *_chapterTitleLabel;
    UILabel *_videoDurationLabel;
    UIButton *_videoPlayButton;
    BOOL _isPlaying;
    BOOL _isFirstPlay;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self loadUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _isFirstPlay = YES;
    }
    return self;
}

- (void)loadUI {
    _topVerticalLine    = [UIView new];
    _middleCircle       = [UIView new];
    _bottomVerticalLine = [UIView new];
    
    _topVerticalLine.backgroundColor = HEXCOLOR(0xeeeeee);
    _middleCircle.backgroundColor = HEXCOLOR(0xdedede);
    _bottomVerticalLine.backgroundColor = HEXCOLOR(0xeeeeee);

    _middleCircle.layer.cornerRadius = 3.5f;
    _middleCircle.clipsToBounds = YES;
    
    // 日期
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.textColor = HEXCOLOR(0x666666);
    _dateLabel.font = CHINESE_SYSTEM(11);

    _dateLabel.backgroundColor = [UIColor clearColor];
    _dateLabel.textAlignment = NSTextAlignmentLeft;
    
    // 章节标题
    _chapterTitleLabel = [[UILabel alloc] init];
//    _chapterTitleLabel.numberOfLines = 0;
    _chapterTitleLabel.font = CHINESE_SYSTEM(13);
    _chapterTitleLabel.textColor = COLOR_WORD_BLACK;
    
    // 播放按钮
    _videoPlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_videoPlayButton setImage:[UIImage imageNamed:kVideoPauseImg] forState:UIControlStateNormal];
    _videoPlayButton.userInteractionEnabled = NO;
    
    _videoDurationLabel = [[UILabel alloc]init];
    _videoDurationLabel.textColor = COLOR_WORD_GRAY_2;
    _videoDurationLabel.font = CHINESE_SYSTEM(12);
    
    [self.contentView addSubview:_topVerticalLine];
    [self.contentView addSubview:_middleCircle];
    [self.contentView addSubview:_bottomVerticalLine];
    [self.contentView addSubview:_dateLabel];
    [self.contentView addSubview:_chapterTitleLabel];
    [self.contentView addSubview:_videoDurationLabel];
    [self.contentView addSubview:_videoPlayButton];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    [_middleCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(71);
        make.height.width.equalTo(@7);
        make.centerY.equalTo(self.contentView);
    }];
    
    // 日期
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(26);
        make.right.equalTo(_middleCircle.mas_left);
        make.centerY.equalTo(self.contentView);
        make.height.width.equalTo(@(kChapterLabW));
    }];
    
    [_topVerticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.bottom.equalTo(_middleCircle.mas_top).offset(-2);
        make.width.equalTo(@1);
        make.centerX.equalTo(_middleCircle);
    }];
    
    [_bottomVerticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_middleCircle.mas_bottom).equalTo(@2);
        make.width.equalTo(_topVerticalLine);
        make.bottom.equalTo(self.contentView);
        make.centerX.equalTo(_middleCircle);
    }];
    
    // 标题
    [_chapterTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_middleCircle.mas_right).offset(kMargin);
        make.top.equalTo(self.contentView).offset(kEdgeMargin);
        make.bottom.equalTo(self.contentView).offset(-1*kEdgeMargin);
        make.right.equalTo(_videoDurationLabel.mas_left).offset(-1*kMargin);
    }];

    // 暂停按钮
    [_videoPlayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-1*kEdgeMargin);
        make.top.equalTo(_dateLabel);
        make.width.height.equalTo(@(kDurationLabelH));
    }];
    
    //  时长
    [_videoDurationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_videoPlayButton.mas_left).offset(-1*kMargin);
        make.top.equalTo(_dateLabel);
        make.height.equalTo(@(kDurationLabelH));
        make.width.equalTo(@(kDurationLabelW));
    }];
    
}

-(void)setIsFirstCell:(BOOL)isFirstCell {
    _isFirstCell = isFirstCell;
    _topVerticalLine.hidden = isFirstCell;
}

- (void)setIsLastCell:(BOOL)isLastCell {
    _isLastCell = isLastCell;
    _bottomVerticalLine.hidden = isLastCell;
}

-(void)setDataModel:(BIZMoviePlaySitcomScrListDataModel *)dataModel {
    _dataModel  = dataModel;
    _dateLabel.text = [[NSString stringWithFormat:@"%@",[NSDate stringFromTimestamp:dataModel.createTime dateFormat:@"yyyy-MM-dd"]] substringWithRange:NSMakeRange(5, 5)];
    _chapterTitleLabel.text = dataModel.title?:@"敬请期待";
    _videoDurationLabel.text = dataModel.remark;
    
    [self layoutIfNeeded];
}

-(void)setIsPlayingVideoID:(NSString *)isPlayingVideoID {
    _isPlayingVideoID = isPlayingVideoID;
    if (![isPlayingVideoID isEqualToString:_dataModel.uuid]) { // 如果不是当前视频在播放中..
        
        //  重置播放状态
        [self setConfigInfoForPlayStatusWithWhetherIsPlaying:NO];
        
    }else{
        
        [self setConfigInfoForPlayStatusWithWhetherIsPlaying:YES];
    }
}

- (void)clickPlayButton {

    // 视频id
    self.block ? self.block(_dataModel.uuid,_isFirstPlay,_dataModel.title) :nil;
    
}

- (void)setConfigInfoForPlayStatusWithWhetherIsPlaying:(BOOL)isPlaying {
    
    _videoPlayButton.enabled = !isPlaying;

    if (isPlaying) {
        _middleCircle.backgroundColor = HEXCOLOR(0xcb1717);
        [_videoPlayButton setImage:[UIImage imageNamed:kVideoPlayImg] forState:UIControlStateDisabled];
    }else {
        _middleCircle.backgroundColor = HEXCOLOR(0xdedede);
        [_videoPlayButton setImage:[UIImage imageNamed:kVideoPauseImg] forState:UIControlStateNormal];
    }
    _chapterTitleLabel.textColor = isPlaying?COLOR_WORD_RED_PLAYER:COLOR_WORD_BLACK;

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
